from collections import namedtuple, defaultdict
import re

from cpswarehouse.model import SchoolDimension
from cpswarehouse.model.meta import Session as session
from cpswarehouse.model.meta import Session


simple_replacements = {'chrt': 'charter',
                       'chrt': 'charter',
                       'chrtr': 'charter',
                       'chartr': 'charter',
                       'chr': 'charter',
                       'chtr': 'charter',
                       'ctr': 'charter',
                       'hs': 'high school',
                       'aca': 'academy',
                       'acd': 'academy',
                       'acad': 'academy',
                       'comm': 'community',
                       'commnty': 'community',
                       'commty': 'community',
                       'coll': 'college',
                       'col': 'college',
                       'schol': 'school',
                       'schl': 'school',
                       'st': 'street',
                       'mil': 'military',
                       'univ': 'university',
                       'chgo': 'chicago',
                       'connctns': 'connections',
                       'yng': 'young',
                       'womn': 'women',
                       'ldr': 'leadership',
                       'ldrshp': 'leadership',
                       'sci': 'science',
                       'scien': 'science',
                       }


def simple_word_replace(schoolname):
    newwords = list()
    for word in schoolname.lower().split():
        word = word.strip()
        if(simple_replacements.has_key(word)):
            newwords.append(simple_replacements[word])
        else:
            newwords.append(word)
    return " ".join(newwords)


def regex_school_replace(schoolname):
    schoolname = re.sub('high$', 'high school', schoolname)

    return schoolname


def simple_school_consolidate():
    schools = Session.query(SchoolDimension).all()

    school_names_to_schools = defaultdict(list)

    for school in schools:
        school_names_to_schools[school.school].append(school)
        
    delete_me = list()
    for school_list in school_names_to_schools.values():
        if(len(school_list) > 1):
            canonical_school = school_list[0]
            
            for school in school_list[1:]:
                for fact in school.facts:
                    fact.school_id = canonical_school.id
                delete_me.append(school)
            session.commit()

    for school in delete_me:
        session.delete(school)
        session.commit()


def consolidate_by_unit():
    schools = Session.query(SchoolDimension).all()

    school_units_to_schools = defaultdict(list)

    for school in schools:
        school_units_to_schools[school.unit].append(school)

    def max_school_name(schools):
        max_school = schools[0]
        for school in schools:
            if(len(school.school)>len(max_school.school)):
                max_school=school
        return max_school

    delete_me = list()
    for school_list in school_units_to_schools.values():
        if(len(school_list) > 1):
            canonical_school = max_school_name(school_list)
            
            for school in [school for school in school_list if school is not canonical_school]:
                for fact in school.facts:
                    fact.school_id = canonical_school.id
                delete_me.append(school)
            session.commit()

    for school in delete_me:
        session.delete(school)
        session.commit()

#def clean_the_data():
#    pass
    
